import { LoginTypeEntity } from "spen-common/src/interface/RequestOperation";
import { User } from "../dao/UserDao";
import { UserEntity } from "spen-common/src/entity/User";

/** 可以缓存到数据库 */
export async function getLoginCode(authen: LoginTypeEntity) : Promise<string> {
    let userEntity : UserEntity | null = null;
    switch(authen.type){
        case "phone-code":{
            let userMode = await User.findOne({where: {phone: authen.body.account}});
            userEntity = userMode == null ? null :  userMode.toJSON();
            return '11';
        }
        case "email-code":{
            let userMode  =  await User.findOne({where: {email: authen.body.account}});
            userEntity = userMode == null ? null : userMode.toJSON();
            return '10';
        }
    }
    return null;
}

export async function login(authen: LoginTypeEntity){
    switch(authen.type){
        case "phone-code":
        case "phone":{
            let user = await User.findOne({where: {
                phone: authen.body.account
            }});
            return user?.toJSON();
        }
        case "email":
        case "email-code":{
            let user = await User.findOne({where: {
                eamil: authen.body.account
            }});
            return user?.toJSON();
        }
        case "code":{
            let user = await User.findOne({where: {
                registry: authen.body.account
            }});
            return user?.toJSON();
        }
    }
}

export function getAuthenCode(): string{
        var str='';
        for(var i=0;i<26;i++){
            str+=String.fromCharCode(65+i);
        }
        for(var i=0;i<26;i++){
            str+=String.fromCharCode(97+i);
        }
        for(var i=0;i<10;i++){
            str+=i;
        }
        return str.at(Math.random() * 100 % 63) + str.at(Math.random() * 100 % 63) + str.at(Math.random() * 100 % 63)
         + str.at(Math.random() * 100 % 63);
}